﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DemoGridView_ShowImageInGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadProduct();
    }

    private void LoadProduct()
    {
        //SqlDataAdapter da = new SqlDataAdapter("select pid,pname,price,picture from Product", DataAccess.ConnectionString);
        //DataSet ds = new DataSet();
        //da.Fill(ds);
        //GridView1.DataSource = ds.Tables[0];
        //GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //SqlDataAdapter da = new SqlDataAdapter("select pid,pname,price,picture from Product where pname like '%" + TextBox1.Text + "%'", DataAccess.ConnectionString);
        //DataSet ds = new DataSet();
        //da.Fill(ds);
        //if (ds.Tables[0].Rows.Count > 0)
        //{
        //    GridView1.DataSource = ds.Tables[0];
        //    GridView1.DataBind();
        //}
        //else
        //{
        //    Response.Write("khong tim thay");
        //    GridView1.Visible = false;
        //}
        //string tensp=TextBox1.Text;


        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = " select pid,pname,price,picture from Product where pname like '%' + @tensp + '%'";
        cmd.Parameters.AddWithValue("@tensp", txtsearch.Text);
        //cmd.Parameters.Add("@tensp", SqlDbType.NVarChar).Value = TextBox1.Text;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            if (dr.Read())
            {
                GridView1.DataSource = dr;
                GridView1.DataBind();
            }

        }
    }
}
